package com.yangmin.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class FlowLimitController {
    @GetMapping("/testA")
    public String testA() {
        return "--------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "---------testB";
    }

    @GetMapping("/testD")
    public String testD() {
        //暂停几秒钟线程
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("testD 测试RT");
        return "------testD";
    }

    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHostKey", blockHandler = "dealHandler_testHotKey")
    public String testHotKey(@RequestParam(value = "v1", required = false) String v1,
                             @RequestParam(value = "v2", required = false) String v2) {
        return "----------testHotKey";
    }

    public String dealHandler_testHotKey(String v1, String v2, BlockException e) {
        return "-----dealHandler_testHotKey";
    }

    @GetMapping("/defineDealHandler")
    @SentinelResource(value = "defineDealHandler", blockHandlerClass = RateDealHandler.class, blockHandler = "handleException2")
    public String defineDealHandler() {
        return "----------defineDealHandler";
    }
}
